package com.fingard.dsp.bank.directbank.cmbc01;

import com.fingard.dsp.bank.directbank.bankInterface.IRetailMsg;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQry;
import com.fingard.dsp.bank.reqrespobj.retailMsg.ReqRetailMsgQryDetail;
import com.fingard.dsp.bank.reqrespobj.retailMsg.RetAtsRetailMsg;
import com.fingard.text.StringHelper;
import com.fingard.xml.XElement;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import org.springframework.stereotype.Component;

@Component("CMBC01RetailMsg")
public class CMBC01RetailMsg extends CMBC01Base implements IRetailMsg{

	@Override
	public RetAtsRetailMsg getRetailMsg(ReqRetailMsgQry p_orderMsg) throws Exception {
		RetAtsRetailMsg retAts = new RetAtsRetailMsg(p_orderMsg.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
		String tmpRespInfo = "";
		try {
			while(p_orderMsg.readRecord()) {
				CMBC01Header bankHeader = new CMBC01Header();
				bankHeader.trnCode = "XR210003";
				XmlTextWriter xmlWriter = new XmlTextWriter();// 发银行报文的XML对象
				// 写入通用的头部节点
				writeCommonHeader(xmlWriter, bankHeader);
				// 写入body
				xmlWriter.writeStartElement("xDataBody");
				ReqRetailMsgQryDetail tmpDetail = p_orderMsg.ReqDetail;

				//xmlWriter.writeElementString("CUST_ID_NO", getBankConfig().getValueAsString("custidno"));// 经销商证件号码
				//xmlWriter.writeElementString("ID_TYPE", getBankConfig().getValueAsString("idtype"));// 经销商证件号码
				xmlWriter.writeElementString("xskhdm",p_orderMsg.DealerCode);//特约店代码
				xmlWriter.writeElementString("xsvinm",tmpDetail.VIN);// VIN
				xmlWriter.writeElementString("kcfdjh",tmpDetail.EngineNo);//发动机号
				xmlWriter.writeElementString("kchgzh",tmpDetail.CertificationNo);//合格证号

				xmlWriter.writeElementString("xsrzdh",p_orderMsg.reqHeader.transSeqID);//申请单号
				xmlWriter.writeElementString("xsxsrq",tmpDetail.RetailInvoiceDate);//实销日期
				xmlWriter.writeElementString("xsxsjg", StringHelper.isNullOrEmpty(tmpDetail.SalePrice) ? "0" : tmpDetail.SalePrice);//实销日期
				xmlWriter.writeEndElement();
				xmlWriter.writeEndDocument();
				String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
				if (tmpStrRet[0].length() == 0) {
					StringBuilder tmpSbMsg = new StringBuilder();
					// 解析银行返回
					// base64解码
					XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
					if (tmpBkReader != null) {
						String ans_code = tmpBkReader.getFirstText("/CMBC/responseHeader/status/code");
						String ans_info = tmpBkReader.getFirstText("/CMBC/responseHeader/status/message");
						if ("0".equals(ans_code)) {
							String rsp_status = tmpBkReader.getFirstText("/CMBC/xDataBody/XTCSBZ");
							String rsp_msg = tmpBkReader.getFirstText("/CMBC/xDataBody/XTSBYY");
							if ("01".equals(rsp_status)) {
								tmpRespCode = "0";
								tmpRespInfo = "交易成功";
							} else {
								tmpRespCode = "-1";
								tmpRespInfo = rsp_msg;
							}
						} else {
							tmpRespCode = "-1";
							tmpRespInfo = ans_info;
						}
					} else {
						tmpRespCode = "-1";
						tmpRespInfo = tmpSbMsg.toString();
					}
				} else {
					tmpRespCode = "-1";
					tmpRespInfo = tmpStrRet[1];
				}
			}
		} catch (Exception e) {
			tmpRespCode = "-1";
			tmpRespInfo = e.getMessage();
			e.printStackTrace();
		}
		retAts.writeRespCode(tmpRespCode);
		retAts.writeRespInfo(tmpRespInfo);
		retAts.writeEnd();
		return retAts;
	}

}
